package com.tl.sun;

import android.content.Context;
import android.content.SharedPreferences;

import com.tl.sun.model.AppEntity;

/**
 * 作者：HQY on 16/10/29 10:07
 * 邮箱：hqy_xz@126.com
 */

public class AppConfig {

    private static AppConfig instance;


    private String NO_FIRST_LANUCH = "first_launch";

    public String BUY_URL = "buy_url";
    public String HELPE_URL = "helpe_url";
    public String INDEX_URL = "index_url";
    public String UCENTER_URL = "ucenter_url";
    public String ACTIVITY_RUL = "activity_url";
    public String ABOUT_RUL = "about_url";
    public String ALLOWANCE = "allowance";
    public String RECORDS = "records";
    public String USER_RECORD = "user_record";
    public String TERMINAL = "terminal";
    public String AGREEMENT = "agreement";
    public String SERVICE_URL = "service_url";

    // Set up wechat app id
    public static final String WX_APP_ID = "wxbe9e20922803051e";
    // Set up wechat app secret
    public static final String WX_APP_SECRET = "ba54b22134fee7cbb0c1d5c0ec6e80fd";

    public static final String ZHIMA = "zhimadaili";

//    public static final String WX_APP_ID = "wx1a7db76a524570bb";


    private final static String APP_CONFIG = "config";
    private Context mContext;

    public static AppConfig getInstance() {
        if (instance == null) {
            synchronized (AppConfig.class) {
                instance = new AppConfig();
            }
        }
        return instance;
    }

    public AppConfig() {
        mContext = AppContext.getInstance();
        getCache();
    }

    private AppEntity appInfo;

    public void getCache() {
        SharedPreferences sp = mContext.getSharedPreferences(APP_CONFIG, Context.MODE_PRIVATE);
        getAPP(sp);
    }


    public void clearCache() {
        SharedPreferences settings = mContext.getSharedPreferences(APP_CONFIG, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.apply();
    }

    public void saveAppCofig(AppEntity appEntity) {
        SharedPreferences settings = mContext.getSharedPreferences(APP_CONFIG, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        appInfo = appEntity;
        saveApp(editor);

        editor.commit();
    }

    public void setHasLaunch(boolean isLauch) {
        SharedPreferences settings = mContext.getSharedPreferences(APP_CONFIG, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(NO_FIRST_LANUCH, isLauch);
        editor.apply();
    }


    public boolean getHasLauch() {
        SharedPreferences sp = mContext.getSharedPreferences(APP_CONFIG, Context.MODE_PRIVATE);
        return sp.getBoolean(NO_FIRST_LANUCH, false);
    }


    private void saveApp(SharedPreferences.Editor editor) {
        editor.putString(BUY_URL, appInfo.getBuyUrl());
        editor.putString(HELPE_URL, appInfo.getHelpUrl());
        editor.putString(INDEX_URL, appInfo.getIndexUrl());
        editor.putString(ACTIVITY_RUL, appInfo.getActivityUrl());
        editor.putString(UCENTER_URL, appInfo.getUcenterUrl());
        editor.putString(ABOUT_RUL, appInfo.getAboutUrl());
        editor.putString(ALLOWANCE, appInfo.getAllowance());
        editor.putString(RECORDS, appInfo.getRecords());
        editor.putString(USER_RECORD, appInfo.getUserRecord());
        editor.putString(TERMINAL, appInfo.getTerminal());
        editor.putString(AGREEMENT, appInfo.getAgreementUrl());
        editor.putString(SERVICE_URL, appInfo.getServiceUrl());

    }

    public void getAPP(SharedPreferences sp) {
        AppEntity appEntity = new AppEntity();
        appEntity.setActivityUrl(sp.getString(ACTIVITY_RUL, ""));
        appEntity.setBuyUrl(sp.getString(BUY_URL, ""));
        appEntity.setHelpUrl(sp.getString(HELPE_URL, ""));
        appEntity.setIndexUrl(sp.getString(INDEX_URL, ""));
        appEntity.setUcenterUrl(sp.getString(UCENTER_URL, ""));
        appEntity.setAboutUrl(sp.getString(ABOUT_RUL, ""));
        appEntity.setAllowance(sp.getString(ALLOWANCE, ""));
        appEntity.setRecords(sp.getString(RECORDS, ""));
        appEntity.setUserRecord(sp.getString(USER_RECORD, ""));
        appEntity.setTerminal(sp.getString(TERMINAL, ""));
        appEntity.setAgreementUrl(sp.getString(AGREEMENT, ""));
        appEntity.setServiceUrl(sp.getString(SERVICE_URL, ""));
    }

    public AppEntity getAppInfo() {
        return appInfo;
    }

    public void setAppInfo(AppEntity appInfo) {
        this.appInfo = appInfo;
    }
}
